package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.Cate;
import com.ruoyi.system.domain.ProjectCheckDetail;
import com.ruoyi.system.domain.res.projectCheck.ProjectCheckInfo_res;
import com.ruoyi.system.domain.res.projectCheck.ProjectCheckList_res;
import com.ruoyi.system.domain.res.projectCheck.ProjectCheckList_union_res;
import com.ruoyi.system.mapper.CateMapper;
import com.ruoyi.system.mapper.ProjectCheckDetailMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ProjectCheckMapper;
import com.ruoyi.system.domain.ProjectCheck;
import com.ruoyi.system.service.IProjectCheckService;

import javax.annotation.Resource;

/**
 * 竞标项目检查Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-22
 */
@Service
public class ProjectCheckServiceImpl implements IProjectCheckService {
    @Resource
    private ProjectCheckMapper projectCheckMapper;
    @Resource
    private CateMapper cateMapper;
    @Resource
    private ProjectCheckDetailMapper projectCheckDetailMapper;

    /**
     * 查询竞标项目检查
     *
     * @param id 竞标项目检查主键
     * @return 竞标项目检查
     */
    @Override
    public ProjectCheck selectProjectCheckById(Long id) {
        return projectCheckMapper.selectProjectCheckById(id);
    }

    /**
     * 查询竞标项目检查_back
     */
    @Override
    public ProjectCheckInfo_res selectProjectCheckById_back(Long id) {
        ProjectCheckInfo_res res = new ProjectCheckInfo_res();
        BeanUtils.copyProperties(formatProject(projectCheckMapper.selectProjectCheckById(id)), res);
        res.setDetailList(projectCheckDetailMapper.selectProjectCheckDetailList(new ProjectCheckDetail() {{
            setProjectId(res.getId());
        }}));
        return res;
    }

    /**
     * 查询竞标项目检查列表
     *
     * @param projectCheck 竞标项目检查
     * @return 竞标项目检查
     */
    @Override
    public List<ProjectCheck> selectProjectCheckList(ProjectCheck projectCheck) {
        return projectCheckMapper.selectProjectCheckList(projectCheck);
    }

    /**
     * 查询竞标项目检查列表_back
     */
    @Override
    public ProjectCheckList_union_res selectProjectCheckList_back(ProjectCheck projectCheck) {
        ProjectCheckList_union_res unionRes = new ProjectCheckList_union_res();
        List<ProjectCheck> list = projectCheckMapper.selectProjectCheckList_back(projectCheck);
        unionRes.setList(list);
        unionRes.setResList(list.stream().map(this::formatProject).collect(Collectors.toList()));
        return unionRes;
    }

    /**
     * 格式化
     */
    private ProjectCheckList_res formatProject(ProjectCheck item) {
        ProjectCheckList_res res = new ProjectCheckList_res();
        res.setId(item.getId());
        res.setTitle(item.getTitle());
        res.setCreateTime(item.getCreateTime());
        res.setCateId(item.getCateId());
        if (item.getCateId() != null) {
            Cate cate = cateMapper.selectCateById(item.getCateId());
            if (cate != null) {
                res.setCateIdStr(cate.getName());
            }
        }
        return res;
    }

    /**
     * 新增竞标项目检查
     *
     * @param projectCheck 竞标项目检查
     * @return 结果
     */
    @Override
    public int insertProjectCheck(ProjectCheck projectCheck) {
        projectCheck.setCreateTime(DateUtils.getNowDate());
        return projectCheckMapper.insertProjectCheck(projectCheck);
    }

    /**
     * 修改竞标项目检查
     *
     * @param projectCheck 竞标项目检查
     * @return 结果
     */
    @Override
    public int updateProjectCheck(ProjectCheck projectCheck) {
        projectCheck.setUpdateTime(DateUtils.getNowDate());
        return projectCheckMapper.updateProjectCheck(projectCheck);
    }

    /**
     * 批量删除竞标项目检查
     *
     * @param ids 需要删除的竞标项目检查主键
     * @return 结果
     */
    @Override
    public int deleteProjectCheckByIds(Long[] ids) {
        return projectCheckMapper.deleteProjectCheckByIds(ids);
    }

    /**
     * 删除竞标项目检查信息
     *
     * @param id 竞标项目检查主键
     * @return 结果
     */
    @Override
    public int deleteProjectCheckById(Long id) {
        return projectCheckMapper.deleteProjectCheckById(id);
    }
}
